package com.heima.wemedia.service.impl;

import com.heima.common.exception.CustException;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.wemedia.dtos.AnalysisDto;
import com.heima.model.wemedia.pojos.WmNewsStatistics;
import com.heima.model.wemedia.vos.FansStatistics;
import com.heima.model.wemedia.vos.WmChartData;
import com.heima.model.wemedia.vos.WmTableData;
import com.heima.model.wemedia.vos.WmTopItems;
import com.heima.wemedia.service.WmStatisticsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class WmStatisticsServiceImpl implements WmStatisticsService {
    @Autowired
    MongoTemplate mongoTemplate;
    @Override
    public ResponseResult news(AnalysisDto dto) throws ParseException {
        //检查参数
        if (dto == null) {
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID);
        }
        //获取user
        ApUser user = AppThreadLocalUtils.getUser();
        if (user == null) {
            CustException.cust(AppHttpCodeEnum.NEED_LOGIN);
        }
        //根据传来的参数查询
        //根据etime和stime 查询
        SimpleDateFormat simp= new SimpleDateFormat("yyyy-MM-dd");
        Date etime = simp.parse(simp.format(dto.getEtime()));
        Date stime = simp.parse(simp.format(dto.getStime()));
        WmChartData wmChartData = new WmChartData();
        WmTopItems wmTopItems = new WmTopItems();
        Map<String, Long> data = new HashMap<>();
        ArrayList<WmTableData> wmTableData = new ArrayList<>();
        long article = 0;
        long readCount = 0;
        long fansReadCount = 0;
        long comment = 0;
        List<WmNewsStatistics> wmNewsStatistics =new ArrayList<>();
        if (dto.getEtime().equals(dto.getStime())){
            wmNewsStatistics= mongoTemplate.find(Query.query(Criteria.where("userId").is(user.getId()).and("creatTime").is(etime)), WmNewsStatistics.class);
        }else {
            wmNewsStatistics= mongoTemplate.find(Query.query(Criteria.where("userId").is(user.getId()).and("creatTime").lte(stime).gte(etime)), WmNewsStatistics.class);

        }


        for (WmNewsStatistics wmNewsStatistic : wmNewsStatistics) {
            article += wmNewsStatistic.getArticle();
            readCount += wmNewsStatistic.getReadCount();
            fansReadCount += wmNewsStatistic.getFansReadCount();
            comment += wmNewsStatistic.getComment();
            Date creatTime = wmNewsStatistic.getCreatTime();
            String format = simp.format(creatTime);
            WmTableData wmTable = new WmTableData();
            wmTable.setDate(format);
            wmTable.setFavNum(wmNewsStatistic.getCollection());
            wmTable.setCommentNum(wmNewsStatistic.getComment());
            wmTable.setForwardNum(81);
            wmTable.setReadNum(wmNewsStatistic.getReadCount());
            wmTable.setFanReadNum(wmNewsStatistic.getFansReadCount());
            wmTableData.add(wmTable);

        }
        data.put("阅读量", readCount);
        data.put("文章量", article);
        data.put("粉丝阅读量", fansReadCount);
        data.put("评论量", comment);
        wmChartData.setData(data);
        wmTopItems.setCommentNum(comment);
        wmTopItems.setArticleNum(article);
        wmTopItems.setReadNum(readCount);
        wmTopItems.setFanReadNum(fansReadCount);
        ArrayList<Object> result = new ArrayList<>();
        result.add(wmChartData);
        result.add(wmTableData);
        result.add(wmTopItems);
        return ResponseResult.okResult(result);

    }

    @Override
    public ResponseResult fans(AnalysisDto dto) {
        //检查参数
        if (dto == null) {
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID);
        }
        //获取user
        ApUser user = AppThreadLocalUtils.getUser();
        if (user == null) {
            CustException.cust(AppHttpCodeEnum.NEED_LOGIN);
        }
        //根据传来的参数查询
        //根据etime和stime 查询
        SimpleDateFormat simp= new SimpleDateFormat("yyyy-MM-dd");
        Date etime = null;
        try {
            etime = simp.parse(simp.format(dto.getEtime()));
        } catch (ParseException parseException) {
            parseException.printStackTrace();
        }
        Date stime = null;
        try {
            stime = simp.parse(simp.format(dto.getStime()));
        } catch (ParseException parseException) {
            parseException.printStackTrace();
        }
        List<WmNewsStatistics> wmNewsStatistics =new ArrayList<>();
        if (dto.getEtime().equals(dto.getStime())){
            wmNewsStatistics= mongoTemplate.find(Query.query(Criteria.where("userId").is(user.getId()).and("creatTime").is(etime)), WmNewsStatistics.class);
        }else {
            wmNewsStatistics= mongoTemplate.find(Query.query(Criteria.where("userId").is(user.getId()).and("creatTime").lte(stime).gte(etime)), WmNewsStatistics.class);

        }
        List<FansStatistics> result = wmNewsStatistics.stream().map(wmNewsStatistics1 -> {
            FansStatistics fansStatistics = new FansStatistics();
            fansStatistics.setArticle(wmNewsStatistics1.getArticle());
            fansStatistics.setRead_count(wmNewsStatistics1.getReadCount());
            fansStatistics.setComment(wmNewsStatistics1.getComment());
            fansStatistics.setForward(52);
            fansStatistics.setLikes(wmNewsStatistics1.getLikes());
            fansStatistics.setFollow(wmNewsStatistics1.getFollow());
            fansStatistics.setUnlikes(wmNewsStatistics1.getUnLikes());
            fansStatistics.setCreated_time(wmNewsStatistics1.getCreatTime());
            return fansStatistics;
        }).collect(Collectors.toList());
        return ResponseResult.okResult(result);
    }
}
